
var prefix = "/fin/reim"
$(function() {
	loadProject();
	load();
	laydate({
		elem : '#reimDate'
	});
});

function load() {
	$('#exampleTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						singleSelect : false, // 设置为true将禁止多选
						// //发送到服务器的数据编码类型
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						showColumns : false, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							return {
								//说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
								limit: params.limit,
								offset:params.offset,
								projectid:$('#projectid').val(),
								reimNumber:$('#searchName').val(),
								reimUsername:$('#reimUsername').val(),
								reimDate:$('#reimDate').val()
							};
						},
						columns : [
								{
									checkbox : true
								},
								{
									visible : false,
									field : 'id', 
									title : '主键' 
								},
								{
									field : 'reimNumber', 
									title : '报销单号',
									formatter: function(value,row,index){
										return '<a href="'+prefix  + '/ckedit/'+row.id+'" target="_blank">'+row.reimNumber+'</a>';
									}
								},
								{
									field : 'status',
									title : '状态',
									formatter : function(value, row, index){
										if(row.status==1){
											return '<span class="label label-info">新创建</span>';
										}else if(row.status==2){
											return '<span class="label label-primary">初审中</span>';
										}else if(row.status==3){
											return '<span class="label label-success">终审中</span>';
										}else if(row.status==4){
											return '<span class="label label-success">待付款</span>';
										}else if(row.status==5){
											return '<span class="label label-success">已付款</span>';
										}else if(row.status==6){
											return '<span class="label label-danger">已删除</span>';
										}
									}
								},
								{
									field : 'reimDate',
									title : '报销日期',
									formatter: function(value,row,index){
										if(row.reimDate==null || row.reimDate ==''){
											return '';
										}else{
											return row.reimDate.substr(0,10);
										}
									}
								},
								{
									field : 'reimUsername',
									title : '报销人'
								},
								{
									field : 'reimAmount', 
									title : '报销金额' ,
									formatter: function(value,row,index){
										var reimAmount =parseFloat(row.reimAmount).toFixed(2);
										return reimAmount;
									}
								},
								{
									field : 'depetLeadername',
									title : '部门领导'
								},
																{
									field : 'gmtCreate', 
									title : '创建时间' 
								},
								{
									title : '操作',
									field : 'operation',
									align : 'center',
									formatter : function(value, row, index) {
										var exp = '<a class="btn btn-sm" href="/fin/reim/exportinfo/'+row.id+'"  title="导出">' +
											'<i class="fa fa-reply"></i></a> ';
										if(row.status == 1){
											var e = '<a class="btn btn-primary btn-sm" href="#" mce_href="#" title="编辑" onclick="edit(\''
												+ row.id
												+ '\')"><i class="fa fa-edit"></i></a> ';
											var d = '<a class="btn btn-danger btn-sm" href="#" mce_href="#" title="删除" onclick="remove(\''
												+ row.id
												+ '\')"><i class="fa fa-trash-o"></i></a> ';
											var c = '<a class="btn btn-success btn-sm" href="#" mce_href="#" title="确认" onclick="confirm(\''
												+ row.id
												+ '\')"><i class="fa fa-check"></i></a> ';
											var h = '<a class="btn btn-sm" href="/fin/reim/printInfo/'+row.id+'"  title="打印" target="_blank">' +
												'<i class="fa fa-print"></i></a> ';
											return e + d + c+h+exp;
										}else if(row.status ==2){
											var f = '<a class="btn btn-warning btn-sm" href="#" mce_href="#" title="初审" onclick="review(\''
												+ row.id
												+ '\')"><i class="fa fa-hand-paper-o"></i></a> ';
											var d = '<a class="btn btn-danger btn-sm" href="#" mce_href="#" title="驳回" onclick="overrule(\''
												+ row.id
												+ '\')"><i class="fa fa-ban"></i></a> ';
											var h = '<a class="btn btn-sm" href="/fin/reim/printInfo/'+row.id+'"  title="打印" target="_blank">' +
												'<i class="fa fa-print"></i></a> ';
											return f + d + h+exp;
										}else if(row.status == 3){
											var f = '<a class="btn btn-warning btn-sm" href="#" mce_href="#" title="终审" onclick="endreview(\''
												+ row.id
												+ '\')"><i class="fa fa-eye"></i></a> ';
											var d = '<a class="btn btn-danger btn-sm" href="#" mce_href="#" title="驳回" onclick="overrule(\''
												+ row.id
												+ '\')"><i class="fa fa-hand-paper-o"></i></a> ';
											var h = '<a class="btn btn-sm" href="/fin/reim/printInfo/'+row.id+'"  title="打印" target="_blank">' +
												'<i class="fa fa-print"></i></a> ';
											return f + d + h+exp;
										}else if(row.status == 4){
											var f = '<a class="btn btn-primary btn-sm '+s_payment_h+'" href="#" mce_href="#" title="付款" onclick="payment(\''
												+ row.id
												+ '\')"><i class="fa fa-paypal"></i></a> ';
											var h = '<a class="btn btn-sm" href="/fin/reim/printInfo/'+row.id+'"  title="打印" target="_blank">' +
												'<i class="fa fa-print"></i></a> ';
											return f +h+exp;
										}
									}
								} ]
					});
}
function reLoad() {
	$('#exampleTable').bootstrapTable('refresh');
}
function add() {
	var page = layer.open({
		type : 2,
		title : '增加',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
	layer.full(page);
}
function edit(id) {
	var page = layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix  + '/edit/' + id // iframe的url
	});
	layer.full(page);
}
function remove(id) {
	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
function review(id) {
	layer.confirm('确定要审核通过？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/review",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
function endreview(id) {
	layer.confirm('确定要审核通过？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/endreview",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
function payment(id) {
	layer.confirm('确定要付款吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/payment",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
function overrule(id) {
	layer.confirm('确定要驳回吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/overrule",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
function confirm(id) {
	layer.confirm('确定要提交审核吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/confirm",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
function exportData() {
	var url =prefix + '/exportData';
	var reimNumber=$('#searchName').val();
	var	reimUsername=$('#reimUsername').val();
	var	reimDate=$('#reimDate').val();
	var projectid=$('#projectid').val();
	if(projectid != null){
		if (url.indexOf('?') > -1) {
			url += '&projectid=' + projectid;
		} else {
			url += '?projectid=' + projectid;
		}
	}
	if(reimNumber != null){
		if (url.indexOf('?') > -1) {
			url += '&reimNumber=' + reimNumber;
		} else {
			url += '?reimNumber=' + reimNumber;
		}
	}
	if(reimUsername != null){
		if (url.indexOf('?') > -1) {
			url += '&reimUsername=' + reimUsername;
		} else {
			url += '?reimUsername=' + reimUsername;
		}
	}
	if(reimDate != null){
		if (url.indexOf('?') > -1) {
			url += '&reimDate=' + reimDate;
		} else {
			url += '?reimDate=' + reimDate;
		}
	}

	var form = document.createElement('form');
	form.setAttribute('action', url);
	form.setAttribute('method', 'post');
	form.setAttribute('style', 'display:none');
	document.body.appendChild(form);
	form.submit();
	document.body.removeChild(form)
}
function loadProject(){
	var html = "";
	$.ajax({
		url : '/itm/project/getNotEndProjectlist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-projectid").append(html);
			$(".chosen-select-projectid").chosen({
				maxHeight : 200,
				search_contains: true
			});
			//点击事件
			$('.chosen-select-projectid').on('change', function(e, params) {
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

function exportDataByProject() {
	var url =prefix + '/exportDataByProject';
	var projectid=$('#projectid').val();
	if(projectid != null){
		if (url.indexOf('?') > -1) {
			url += '&projectId=' + projectid;
		} else {
			url += '?projectId=' + projectid;
		}
	}
	var form = document.createElement('form');
	form.setAttribute('action', url);
	form.setAttribute('method', 'post');
	form.setAttribute('style', 'display:none');
	document.body.appendChild(form);
	form.submit();
	document.body.removeChild(form)
}
